package com.chj.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chj.constants.Constants;
import com.chj.entity.User;
import com.chj.service.MessageService;
import com.chj.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private MessageService messageService;

    @RequestMapping("login")
    public String login(){

        return "user/login";
    }

    @RequestMapping("login_submit")
    public String loginSubmit(HttpServletRequest request
                            , String userId
                            , String userPassword){

        HttpSession session = request.getSession();

        if (userService.login(userId, userPassword)) {
            User user = userService.getUser(userId, userPassword);
            session.setAttribute(Constants.LOGIN_USER, user);
            return "index/index";
        } else {
            return "user/login_fail";
        }
    }

    @RequestMapping("register")
    public String register(){

        return "user/register";
    }
    
    @RequestMapping("register_submit")
    public String registerSubmit(String username
                               , String email
                               , String tel
                               , String password
                               , String checkpassword
                               , HttpServletRequest request){
        
        HttpSession session = request.getSession();
        
        User registerUser = new User(username
                                   , username
                                   , password
                                   , Constants.USER_TYPE_NORMAL
                                   , Constants.USER_ENABLE
                                   , tel
                                   , email);

        if (userService.checkUserExist(registerUser)){
            return "user/registerFail";
        } else if (userService.addUser(registerUser)) {
            User loginUser = userService.getUser(username, password);
            session.setAttribute(Constants.LOGIN_USER, loginUser);
            
            messageService.sendMsg(loginUser.getUserPk(), "用户注册成功！！！欢迎使用本系统");
            return "index/index";
        } else {
            return "user/register_fail";
        }
    }

    @RequestMapping(value="user_exit")
    public String userExit(HttpServletRequest request){

        HttpSession session = request.getSession();

        User loginUser = (User) session.getAttribute(Constants.LOGIN_USER);
        
        if (loginUser != null) {
            session.removeAttribute(Constants.LOGIN_USER);
        }

        return "index/index";
    }

    @RequestMapping(value = "user_isexist")
    @ResponseBody
    public String checkUserIsExist(String userName){

        User user = new User();
        user.setUserId(userName);
        user.setUserName(userName);

        if (userService.checkUserExist(user)){
            return "true";
        } else {
            return "false";
        }
    }
}
